/**   
 * File：LocalBeanFactory.java   
 *   
 * Version：   
 * Date：2014年10月9日     
 * Copyright Clarify:   
 *   
 */

package com.sitech.common;

import com.sitech.jcf.core.exception.BusiException;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

/**
 * @Description: 当前JVM SpringBean方法调用，用于调用订单受理提供的实体方法
 * @author: zhangzza
 * @createTime: 2014年10月9日 上午10:47:21
 */

public class LocalBeanFactory
{
	private static final String ERR_CODE = "9999999";
	// 服务接口名规则是"_"为分隔符
	private static final String SPLIT_CHAR = "_";

	/**
	 * @Description: 实体方法调用（返回参数类型为Map）
	 * @author: zhangzza
	 * @createTime: 2014年10月9日 上午11:53:50
	 * 
	 * @param
	 * @return Map
	 * @throws BusiException 
	 */
	public static Map invokeRefBean1(String serviceName, Map in) throws BusiException
	{
		System.out.println("invokeRefBean1");
		return invokeRefBean1(serviceName, in, Map.class);
	}
	
	public static Map invokeRefBean(String invokeBean)
	{
		System.out.println("invokeRefBean");
		return null;
	}
	
	public static Map test3()
	{
		System.out.println("test3");
		return null;
	}

	/**
	 * 
	 * @Description: 实体方法调用（返回参数类型动态）
	 * @author: zhangzza
	 * @createTime: 2014年10月9日 下午2:56:57
	 * 
	 * @param
	 * @return T
	 * @throws BusiException 
	 */
	public static <T> T invokeRefBean1(String serviceName, Object in, Class<T> clazz) throws BusiException
	{
		T result = null;
		int splitIdx = 0;
		splitIdx = serviceName.lastIndexOf(SPLIT_CHAR);
		if (splitIdx != -1)
		{
			String methodName = serviceName.substring(serviceName.lastIndexOf(SPLIT_CHAR) + 1);
			String interfaceName = serviceName.substring(0, splitIdx);
			splitIdx = interfaceName.lastIndexOf(SPLIT_CHAR);
			if (splitIdx != -1)
			{
				String shortInterfaceName = interfaceName.substring(splitIdx + 1);
				// 行业部设计:spring bean id是接口名称的第二位小写及第三位后所有字符+"Svc",比如IExchgCard
				// --> exchgCardSvc
				String beanName = shortInterfaceName.substring(0, 1).toLowerCase() + shortInterfaceName.substring(1);

				boolean existBean= true;
				if (true == existBean)
				{
					Object o = new Object();
					// 服务接口约定就一个参数，即报文json
					Class[] paraClass = new Class[1];
					if (in instanceof Map)
					{
						paraClass[0] = Map.class;
					}
					else if (in instanceof List)
					{
						paraClass[0] = List.class;
					}
					else
					{
						paraClass[0] = in.getClass();
					}

					try
					{
						Method m = o.getClass().getMethod(methodName, paraClass);
						Object rtnObject = m.invoke(o, in);
						if (rtnObject != null)
						{
							result = (T)clazz.cast(rtnObject); //不强转会报错 父类不允许直接改称子类
						}
					}
					catch (NoSuchMethodException e)
					{
						e.printStackTrace();
						throw new BusiException(ERR_CODE, "实体方法[" + serviceName + "]未找到");
					}
					catch (SecurityException e)
					{
						e.printStackTrace();
						throw new BusiException(ERR_CODE, "无法定位登录配置");
					}
					catch (IllegalAccessException e)
					{
						e.printStackTrace();
						throw new BusiException(ERR_CODE, "安全权限异常");
					}
					catch (IllegalArgumentException e)
					{
						e.printStackTrace();
						throw new BusiException(ERR_CODE, "向方法传递了一个不合法或不正确的参数");
					}
					catch (InvocationTargetException e)
					{
						throw new BusiException(ERR_CODE, "调用方法[" + serviceName + "]失败，错误信息:" + e.getCause());
					}
				}
				else
				{
					throw new BusiException(ERR_CODE, "spring bean[" + beanName + "]未找到");
				}
			}
		}
		return result;
	}

}
